package cn.chint.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IndexController {
    private static final Logger logger = LogManager.getLogger(AdminController.class);

    @Value("${server.port}")
    private int port;
    @Autowired
    RestTemplate restTemplate;

    @CrossOrigin
    @RequestMapping("equipmentdata")
    @ResponseBody
    public String testRest(HttpServletResponse response, HttpServletRequest request){
        JSONObject jsonObj = new JSONObject();
        try {
            HttpSession session = request.getSession();
//				// 2.将用户名保存在session中
            Object token=session.getAttribute("token");
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            headers.set("Authorization",token.toString());

            HttpEntity<String> entity = new HttpEntity<>(headers);

            String uri ="https://cc-common.chintcloud.net/common-api/api/v1.0/device-manage/devices/statistical";
            //Map  m = restTemplate.exchange(uri, HttpMethod.POST,entity,Map.class).getBody();
            Map m = restTemplate.exchange(uri, HttpMethod.GET, entity, Map.class).getBody();
            String code=m.get("code").toString();
            String msg=m.get("msg").toString();
            if("200".equals(code)){
                jsonObj.put("status", true);
                Map<String,Object> mapdata=(Map) m.get("data");
                jsonObj.putAll(mapdata);return  jsonObj.toString();
            }else{
                jsonObj.put("status", false);
                jsonObj.put("msg", "数据获取失败。");

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  jsonObj.toString();
    }


    @CrossOrigin
    @RequestMapping("alarmdata")
    @ResponseBody
    public String alarmRest(@RequestParam(value="entityType",required = false) String entityType,
                            @RequestParam(value="entityId",required = false) String entityId,
                            @RequestParam(value="limit",required = false) String limit,
                            HttpServletResponse response, HttpServletRequest request){
        JSONObject jsonObj = new JSONObject();

        try {
            HttpSession session = request.getSession();
//				// 2.将用户名保存在session中
            Object token=session.getAttribute("token");
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            headers.set("Authorization",token.toString());
            Map<String,Object> map = new HashMap<>();
            map.put("entityType","DEVICE");

            map.put("entityId","d2798aa0-c7cb-11e9-8ca7-19fa7262fa69");
            map.put("limit","2");

          //  HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(map),headers);
            HttpEntity<String> entity = new HttpEntity<>(headers);
            String uri ="https://cc-common.chintcloud.net/common-api/api/v1.0/device-manage/devices/alarm?entityType="+
                    map.get("entityType")+"&entityId="+map.get("entityId")+"&limit="+map.get("limit");
            //Map  m = restTemplate.exchange(uri, HttpMethod.POST,entity,Map.class).getBody();
            Map m = restTemplate.exchange(uri, HttpMethod.GET, entity, Map.class).getBody();
            String code=m.get("code").toString();
            String msg=m.get("msg").toString();
            if("200".equals(code)){
                jsonObj.put("status", true);
                Map<String,Object> mapdata=(Map) m.get("data");
                jsonObj.putAll(mapdata);
            }else{
                jsonObj.put("status", false);
                jsonObj.put("msg", "数据获取失败。");

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  jsonObj.toString();
    }
    @CrossOrigin
    @RequestMapping("clients")
    @ResponseBody
    public String client(HttpServletResponse response, HttpServletRequest request){
        JSONObject jsonObj = new JSONObject();
        try {
            HttpSession session = request.getSession();
//          // 2.将用户名保存在session中
            Object token=session.getAttribute("token");
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            headers.set("Authorization",token.toString());
            HttpEntity<String> entity = new HttpEntity<>(headers);
            String uri ="https://cc-common.chintcloud.net/common-api/api/v1.0/device-manage/devices/alarm?entityType=DEVICE&entityId=d2798aa0-c7cb-11e9-8ca7-19fa7262fa69&limit=2";
            Map m = restTemplate.exchange(uri, HttpMethod.GET, entity, Map.class).getBody();
            String code=m.get("code").toString();
            String msg=m.get("msg").toString();
            if("200".equals(code)){
                jsonObj.put("status", true);
                 Map<String,Object> mapdata=(Map) m.get("data");
                jsonObj.putAll(mapdata);
            }else{
                jsonObj.put("status", false);
                jsonObj.put("msg", "数据获取失败。");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  jsonObj.toString();
    }
    @CrossOrigin
    @RequestMapping("/limit")
    @ResponseBody
    public String limit(HttpServletResponse response, HttpServletRequest request){
        JSONObject jsonObj = new JSONObject();
        try {
            HttpSession session = request.getSession();
//          // 2.将用户名保存在session中
            Object token=session.getAttribute("token");
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            headers.set("Authorization",token.toString());
            HttpEntity<String> entity = new HttpEntity<>(headers);
            String uri ="https://cc-common.chintcloud.net/common-api/api/v1.0/user-center/users/page?pageNo=1&limit=5";
            Map m = restTemplate.exchange(uri, HttpMethod.GET, entity, Map.class).getBody();
            String code=m.get("code").toString();
            String msg = m.get("msg").toString();
            if("200".equals(code)){
                jsonObj.put("status", true);
                 Map mapdata=(Map) m.get("data");
                jsonObj.putAll(mapdata);
            }else{
                jsonObj.put("status", false);
                jsonObj.put("msg", "数据获取失败。");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  jsonObj.toString();
    }


    @CrossOrigin
    @RequestMapping("/del")
    @ResponseBody
    public String del(HttpServletResponse response, HttpServletRequest request, @RequestParam(value="s_form_data",required = true)String s_form_data){
        JSONObject jsonObj = new JSONObject();
        try {
            HttpSession session = request.getSession();
//          // 2.将用户名保存在session中
            Object token=session.getAttribute("token");
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            headers.set("Authorization",token.toString());
            JSONObject jb  = JSONObject.parseObject(s_form_data);
            String id=jb.getString("id");
            String address=jb.getString("address");
            Map<String,Object> map = new HashMap<>();
            map.put("id",id);
            map.put("address",address);
            HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(map),headers);
//            Map map = new HashMap();
//            map.put("id","1ea0f40d8486c52aa9db720f4e30d95");
//            map.put("address","上海市松江区思贤路3255号");
//            id = map.get("id").toString();
//            addresss = (String)map.get("address");
            String uri ="https://cc-common.chintcloud.net/common-api/api/v1.0/user-center/users";
//            Map m = restTemplate.exchange(uri, HttpMethod.PUT, entity, Map.class).getBody();
//            String code=m.get("code").toString();
//            String msg = m.get("msg").toString();
//            if("200".equals(code)){
//                jsonObj.put("status", true);
//                Map mapdata=(Map) m.get("data");
//                jsonObj.putAll(mapdata);
//            }else{
//                jsonObj.put("status", false);
//                jsonObj.put("msg", "数据获取失败。");
//            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  jsonObj.toString();
    }

}
